package com.example.java.service;

import com.example.java.pojo.User;
import com.example.java.pojo.UserAddress;
import com.example.java.pojo.dto.UserDto;
import com.example.java.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService{

    @Autowired
    UserRepository userRepository;

    //用户注册
    @Override
    public User registerUser(UserDto user) {

        User userPojo = new User();

        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        //先查询这个手机号是否已经注册

        if(userRepository.findByPhone(userPojo.getPhone()) == 1)
        {
            //如果返回了1，根据手机号找到了用户，那么返回给前端用户已经存在
            throw new IllegalArgumentException(" user exits");
        }else {
            return userRepository.save(userPojo);
        }
    }

    //用户登录
    @Override
    public User loginUser(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        if(userRepository.findByPhone(userPojo.getPhone()) == 1)
        {
            //先判断该账户是否启用
            if (userRepository.findByPhoneReturnAll(userPojo.getPhone()).getEnabled() == 0)
            {
                throw new IllegalArgumentException("user is not enabled");
            }
            //如果根据手机号找到了用户，那么去检验密码是否匹配
            String returnPassword = userRepository.findByPhoneReturnAll(userPojo.getPhone()).getPassword();
            if(returnPassword.equals(userPojo.getPassword())){
                //如果匹配，那么返回整个对象
                return userRepository.findByPhoneReturnAll(user.getPhone());
            }else {
                //如果不匹配，那么就返回给前端用户密码不匹配
                throw new IllegalArgumentException("password or phone is wrong");
            }
        }else {
            //如果根据手机号没有查到用户，那么返回给前端用户不存在
            throw new IllegalArgumentException("user does not exit, please register");
        }
    }

    //查询用户所有信息
    @Override
    public User getUserinfo(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        //直接查询到后直接返回所有信息
        return userRepository.findByPhoneReturnAll(userPojo.getPhone());
    }

    //添加一个用户
    @Override
    public User addUser(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        //先查询这个手机号是否已经注册
        if(userRepository.findByPhone(userPojo.getPhone()) == 1)
        {
            //如果返回了1，根据手机号找到了用户，那么返回给前端用户已经存在
            throw new IllegalArgumentException(" user exits");
        }else {
            return userRepository.save(userPojo);
        }
    }

    //禁用用户
    @Override
    public User banUser(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        return userRepository.save(userPojo);//传值后直接修改用户信息
    }

    //删除一个用户
    @Override
    public void deleteUser(Integer id) {
        userRepository.deleteById(id);
    }

    //查询一个账户的所有有效地址
    @Override
    public List<UserAddress> getUserAllAddresses(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型
        //直接查询到后直接返回该账户的所有有效地址
        return userRepository.findByPhoneReturnAll(userPojo.getPhone()).getAddresses();
    }

    @Override
    public User getUser(Integer id) {
        return userRepository.findById(id).orElseThrow(() -> {
            throw new IllegalArgumentException("用户不存在，参数异常！");
        });
    }
    //修改一个用户的信息
    @Override
    public User editUser(UserDto user) {

        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);// 将a类型转换为b类型

        return userRepository.save(userPojo);
    }

}
